package au.edu.tardis.tardis_portal;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XslTranslator
{
	
	private Document doc;
	private String xmlns;
	
	public String getXmlns()
	{
		return xmlns;
	}

	public XslTranslator(String xmlString)
	{
		DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
		domFactory.setNamespaceAware(true); // never forget this!
		DocumentBuilder builder;
		try
		{
			builder = (DocumentBuilder) domFactory.newDocumentBuilder();
			this.doc = builder.parse(new InputSource(new StringReader(xmlString)));
			
			this.xmlns = this.doc.getFirstChild().getNamespaceURI();
			
		} catch (ParserConfigurationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (SAXException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
	}	

	public String translate(String xsltString) {
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();

		TransformerFactory tFactory = TransformerFactory.newInstance();

		StringReader sr = new StringReader(xsltString);

		Transformer transformer;
		try
		{
			transformer = tFactory
					.newTransformer(new javax.xml.transform.stream.StreamSource(sr));
			
			transformer.transform(new javax.xml.transform.dom.DOMSource(doc),
					new javax.xml.transform.stream.StreamResult(byteOut));			
		} catch (TransformerConfigurationException e)
		{
			return "transform configuration error";
		} catch (TransformerException e)
		{
			return "transform error";
		}

		return byteOut.toString();
	}
	
	public static void main(String[] args)
	{
		String resultString = "";
		try
		{
			String xmlString = readFileAsString("/Users/steve/Desktop/xmlString");
			String xslString = readFileAsString("/Users/steve/Desktop/xsltString");

			XslTranslator xslt = new XslTranslator(xmlString);
			
			resultString = xslt.translate(xslString);
			
		} catch (IOException e)
		{
			resultString = "error";
			e.printStackTrace();
		}

		System.out.println(resultString);		
	}
	
    /** @param filePath the name of the file to open. Not sure if it can accept URLs or just filenames. Path handling could be better, and buffer sizes are hardcoded
	    */ 
	    private static String readFileAsString(String filePath)
	    throws java.io.IOException{
	        StringBuffer fileData = new StringBuffer(1000);
	        BufferedReader reader = new BufferedReader(
	                new FileReader(filePath));
	        char[] buf = new char[1024];
	        int numRead=0;
	        while((numRead=reader.read(buf)) != -1){
	            fileData.append(buf, 0, numRead);
	        }
	        reader.close();
	        return fileData.toString();
	    }
	
	
	
}